//package com.cyl.h5.controller;
//
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.util.Date;
//import java.util.Enumeration;
//import java.util.HashMap;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import com.alibaba.fastjson.JSONObject;
//import com.cyl.h5.domain.dto.PayNotifyMessageDTO;
//import com.cyl.h5.service.H5OrderService;
//import com.cyl.manager.oms.service.AftersaleService;
//import com.cyl.wechat.WechatPayConfig;
//import com.cyl.yeepay.vo.PayNotifyRetVo;
//import com.google.common.collect.Maps;
//import com.ruoyi.common.utils.JsonUtils;
//import com.wechat.pay.java.core.Config;
//import com.wechat.pay.java.core.notification.NotificationConfig;
//import com.wechat.pay.java.core.notification.NotificationParser;
//import com.wechat.pay.java.core.notification.RequestParam;
//import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
//import com.wechat.pay.java.service.refund.model.RefundNotification;
//import com.yeepay.yop.sdk.http.YopContentType;
//import com.yeepay.yop.sdk.service.common.YopCallbackEngine;
//import com.yeepay.yop.sdk.service.common.callback.YopCallback;
//import com.yeepay.yop.sdk.service.common.callback.YopCallbackRequest;
//import com.yeepay.yop.sdk.service.trade.request.RefundFastRequest;
//import com.yeepay.yop.sdk.service.trade.response.RefundResponse;
//
//import cn.hutool.json.JSONUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//
///**
// * 订单表Controller
// * 
// * @author sjm
// * @date 2023-04-05
// */
//@Api(description ="易宝回调接口列表")
//@RestController
//@RequestMapping("/no-auth/yee")
//public class YeePayNotifyController {
//    private static final Logger log = LoggerFactory.getLogger(YeePayNotifyController.class);
// 
//    @Autowired
//    private H5OrderService h5OrderService;
//    @Autowired
//    private AftersaleService aftersaleService;
//    
//    /** 
//     * 易宝支付回调
//     * @param request
//     * @return 
//     * @throws IOException 
//     */
//    @PostMapping("/pay/notify")
//    public Map<String, String> yeePayNotify(HttpServletRequest request)   {
//    	final String contentTypeStr = request.getContentType();
//        if (!StringUtils.startsWith(contentTypeStr, "application/json")) {
//            throw new RuntimeException("SM2回调请求仅支持json格式");
//        }
//        String reqMethod = request.getMethod();
//        String reqURI = request.getRequestURI();
//        Map<String, String> reqHeaders = getHeaders(request);
//        String jsonReqBody = null;
//		try {
//			jsonReqBody = org.apache.commons.io.IOUtils.toString(request.getInputStream(), "UTF-8");
//		} catch (IOException e) {
//			e.printStackTrace();
//            throw new RuntimeException("SM2回调请求 IOUtils.toString error"+e.getMessage());
//		}
//        String json = decryptSm2Callback(reqMethod, reqURI, reqHeaders, jsonReqBody);
//        PayNotifyRetVo payNotifyRetVo = JSONUtil.toBean(json, PayNotifyRetVo.class);
//        if(payNotifyRetVo==null) {
//            throw new RuntimeException("订单支付回调异常 payNotifyRetVo is null");
//        }
//     	if(payNotifyRetVo.getOrderId() == null) {
//            throw new RuntimeException("订单支付回调异常 payId is null");
//    	}
//        Long payId =   Long.parseLong(payNotifyRetVo.getOrderId());
//     	String status = payNotifyRetVo.getStatus();
//     	if(status==null) {
//            throw new RuntimeException("订单支付回调异常 status is null");
//     	}
//     	Map<String, String> resultMap = new HashMap<String, String>();
//		if(h5OrderService.yeePayCallBack(payNotifyRetVo,json)) {
//			resultMap.put("result", "SUCCESS");
//			return resultMap;
//		};
//		resultMap.put("result", "FAIL");
//		return resultMap;
//		
//    }
//    
//    
//    
//    /** 
//     * 	易宝退款回调
//     * @param request
//     * @return 
//     * @throws IOException 
//     */
//    @PostMapping("/ret/notify")
//    public ResponseEntity<String> yeeRetNotify(HttpServletRequest request)   {
//    	final String contentTypeStr = request.getContentType();
//        if (!StringUtils.startsWith(contentTypeStr, "application/json")) {
//            throw new RuntimeException("SM2回调请求仅支持json格式");
//        }
//        String reqMethod = request.getMethod();
//        String reqURI = request.getRequestURI();
//        Map<String, String> reqHeaders = getHeaders(request);
//        String jsonReqBody = null;
//		try {
//			jsonReqBody = org.apache.commons.io.IOUtils.toString(request.getInputStream(), "UTF-8");
//		} catch (IOException e) {
//			e.printStackTrace();
//            throw new RuntimeException("SM2回调请求 IOUtils.toString error"+e.getMessage());
//		}
//        String json = decryptSm2Callback(reqMethod, reqURI, reqHeaders, jsonReqBody);
//        PayNotifyRetVo payNotifyRetVo = JSONUtil.toBean(json, PayNotifyRetVo.class);
//        if(payNotifyRetVo==null) {
//            throw new RuntimeException("订单支付回调异常 payNotifyRetVo is null");
//        }
//     	if(payNotifyRetVo.getOrderId() == null) {
//            throw new RuntimeException("订单支付回调异常 payId is null");
//    	}
//        Long payId =   Long.parseLong(payNotifyRetVo.getOrderId());
//     	String status = payNotifyRetVo.getStatus();
//     	if(status==null) {
//            throw new RuntimeException("订单支付回调异常 status is null");
//     	}
////     	RefundFastRequest 
////     	 //获取退款回调的信息
////        RefundNotification refundInfo  = new RefundNotification();
////        aftersaleService.refundOrderExc(refundInfo);
////		return h5OrderService.yeePayCallBack(payNotifyRetVo);
//     	return null;
//		
//    }
//    
//    
//    
//    
//    
//    
//    
//    
//    
//    /**
//     * 基于上一步接收到的原始报文，解密后得到业务数据
//     * 
//     * @param reqMethod 请求方法，如POST
//     * @param reqURI 请求路径, 如/rest/xxx，不包含query参数
//     * @param reqHeaders 请求头
//     * @param jsonReqBody json请求体
//     */
//    private String decryptSm2Callback(String reqMethod, String reqURI, 
//        Map<String, String> reqHeaders, String jsonReqBody) {
//    	System.err.println("reqMethod"+reqMethod);
//    	System.err.println("reqURI"+reqURI);
//    	System.err.println("jsonReqBody"+jsonReqBody);
//    	System.err.println("reqHeaders"+JsonUtils.toJsonString(reqHeaders));
//    	log.error("reqMethod"+reqMethod);
//    	log.error("reqURI"+reqURI);
//    	log.error("jsonReqBody"+jsonReqBody);
//    	log.error("reqHeaders"+JsonUtils.toJsonString(reqHeaders));
//        YopCallbackRequest callbackReq = new YopCallbackRequest(reqURI, reqMethod)
//                .setContentType(YopContentType.JSON)
//                .setHeaders(reqHeaders).setContent(jsonReqBody);
//
//        YopCallback callback = YopCallbackEngine.parse(callbackReq);
//        return callback.getBizData();
//    }
//
//    // 获取请求头
//    private Map<String, String> getHeaders(HttpServletRequest request) {
//        Map<String, String> result = Maps.newHashMap();
//        final Enumeration<String> headerNames = request.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            final String headerName = headerNames.nextElement();
//            final String headerValue = request.getHeader(headerName);
//            result.put(headerName, headerValue);
//        }
//        return result;
//    }
//    
//    
//  
//
//}
